﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using XFControls.XFPanels.XFPanelItems;
using XFControls;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using XFControls.XFPanels;

namespace XFSenseUI.XFItems.SenseControls
{
    public class SenseControlNumberModal : SenseItemBase
    {
        private int _height;
        private bool _down;
        private bool _increase;
        private bool _close;

        public int Value { get; set; }

        public SenseControlNumberModal()
        {
            Style = new XFItemStyle()
            {
                TextFont = new Font(FontFamily.GenericSansSerif, 22, FontStyle.Regular),
                TextColor = Color.Black,
                Padding = 15,
                PaddingTop = 18
            };

            ItemRectangle = new Rectangle();
            _height = -1;
            ItemType = XFItemType.Clickable;
        }

        public override int GetHeight()
        {
            return Parent.Parent.Height;
        }

        public override void ResetHeight() { }

        public override void ItemPaint(System.Drawing.Graphics g, int x, int y)
        {
            g.DrawBlackout(Parent.Parent.ClientRectangle);
            int localX = GetWidth() / 2;
            int localY = GetHeight() / 2;

            var rec = this.GetImageRec("counter-two-modal");
            localX -= rec.Width / 2;
            localY -= rec.Height / 2;
            rec.X = localX;
            rec.Y = localY;
            ItemRectangle = rec;

            this.DrawUIAlphaChannel("counter-two-modal", g, ItemRectangle);

            using (SolidBrush b = new SolidBrush(Style.TextColor))
                g.DrawString(Value.ToString(), Style.TextFont, b, localX + Style.PaddingLeft, localY + Style.PaddingTop);
        }

        public override XFItemClickResult GetClickResult()
        {
            if (_close)
                Parent.ClearEffect(this);

            return XFItemClickResult.Nothing;
        }

        public override void OnMouseDown(int x, int y)
        {
            base.OnMouseDown(x, y);

            if (!ItemRectangle.Contains(new Point(x, y)))
            {
                _close = true;
                return;
            }
            _close = false;

            _down = true;
            int half = GetHeight() / 2;

            _increase = y < half;

            while (Parent.State != States.Idle)
            {
                Value += _increase ? 1 : -1;

                if (Value >= 10)
                    Style.PaddingLeft = 10;
                else if (Value >= 100)
                    Style.PaddingLeft = 0;
                else
                    Style.PaddingLeft = 15;

                lock (this)
                {
                    if (Buffer != null)
                        Buffer.Dispose();

                    Buffer = null;
                }
                if (Parent != null)
                    Parent.Invalidate();

                Thread.Sleep(200);
                Application.DoEvents();
            }
        }

        public override void OnBlur()
        {
            base.OnBlur();
            _down = false;
        }

        public override void OnMouseMove(int x, int y)
        {
            base.OnMouseMove(x, y);
            int half = GetHeight() / 2;

            _increase = y < half;
        }

        public override void OnMouseUp()
        {
            base.OnMouseUp();
            _down = false;
            if (_close)
                Parent.ClearEffect(this);
        }
    }
}
